/*!
 * Users edit.
 * Date: 2015/12/29.
 * Copyright© 2014-2015 Vamaker All Rights Reserved.
 */
require([
    'jquery',
		'pageInit',
		'formValidator',
    'formAjax',
    'notification',
    'select2'
	], function ($, vamakerPage, formValidator, formAjax, VamOpMsg, select2) {
  $(function () {
    $('#advertiser_resource').select2();
    $('#govern_users').select2();
    $('#filter_media').select2();
    $('#filter-traffic').select2();
    $('#filter-channel').select2();
  });
  // $(function () {
  //   var $dataFrm = $('#data-frm'),
  //       $trafficFilter = $('#filter-traffic'),
  //       $mediaFilter = $('#filter_media');
  //       $optSelect = $mediaFilter.find('optgroup').remove();
  //     $trafficFilter.on('change', function(){
  //       var $trafficSelected = $trafficFilter.find('option:selected');
  //       $mediaFilter.find('optgroup').remove();
  //       $trafficSelected.each(function(){
  //         var $me = $(this), flag,
  //           meText = $me.text(),
  //           $mediaGroup = $optSelect.filter('optgroup[label="' + meText + '"]');
  //           if (meText === '全部') {
  //             $mediaFilter.append($optSelect);
  //             return false;
  //           };
  //           $mediaFilter.append($mediaGroup);
  //       });
  //     });
  // });
  $(function () {
    var $dataFrm = $('#data-frm'),
        $submitBtn = $dataFrm.find('button[type="submit"]'),
        dataFieldRule, options, dataValidator;
    // console.log($dataFrm);
    dataFieldRule = {
      user_email:{
        rule:{
            required:{
                message: VAM.j('The login email can not be empty')
            },
            email:{
                message: VAM.j('Please enter the correct email')
            }
        },
        messageContainer:'user_email_err'
      },
      user_name:{
        rule:{
            required:{
                message: VAM.j('Username cannot be empty')
            },
            username:{
                message: VAM.j('Username only be letter、number、line-through、underline、And begin with letter')
            },
            maxlength:{
                param: 32,
                message: VAM.j('Mailbox for a maximum of 200 characters')
            }
        },
        messageContainer:'user_name_err'
      }
    };
    options = {
      submitOnSucceed: false,
      onvalidate: function(evt) {
        if (evt.resultList.length <= 0) {
            $dataFrm.ajaxSubmit({
                beforeSubmit: function() {
                  $submitBtn.button('loading');
                },
                success: function (data) {
                  $submitBtn.button('reset');
                  if (data.status === 'success') {
                    VamOpMsg.messager(data);
                    window.location.href = 'user-list';
                  } else {
                    VamOpMsg.messager(data);
                  }
                }
            });
        };
      }
    };
    formValidator.validate($dataFrm, dataFieldRule, options);
    dataValidator = formValidator.dataValidator;
    dataValidator.addRule('password1', function(val, opt){
        opt.trigger('change');
        return true;
      });
    dataValidator.addRule('password2', function(val, opt){
      return val===opt.val();
    });
    dataValidator.addRule('effectiveNumber',function(val, opt){
      return !!(val && /^\d+(\.\d{1,2})?$/.test(val));
    });
    dataValidator.addRule('required', function(val, opt){
      if(opt==='password'){
          return !!(val&&!/^(?:\s\w|\u3000)+$/.test(val));
      }else{
          return !!(val&&!/^(?:\s|\u3000)+$/.test(val));
      }
    });
  });
});
